﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using X3DXNA.core.Controllers;

namespace X3DXNA.hud
{
    public class HUD : DrawableGameComponent
    {
        ContentManager content;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        ShapeController shapeController;

        int frameRate = 0;
        int frameCounter = 0;
        int updateCounter = 0;
        int updateRate = 0;

        public bool showHUD { get; set; }
        public Vector2 setMousePosition { set { mousePositionX = value.X.ToString(); mousePositionY = value.Y.ToString(); } }
        public String CoreUpdate = "Wait...";
        public String CoreDraw = "Wait...";
        public String InputControllerUpdate = "Wait...";

        String fps = "Wait...", ups = "Wait...";
        String drewModels = "Wait...", notDrewModels = "Wait...";
        String mousePositionX = "Wait...", mousePositionY = "Wait...";
        TimeSpan elapsedTime = TimeSpan.Zero;

         public HUD(Game game, ShapeController shapeController)
            : base(game)
        {
            game.Components.Add(this);
            content = new ContentManager(game.Services);
            this.shapeController = shapeController;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = content.Load<SpriteFont>("Content\\Fonts\\Font");
        }

        #region Update & Draw

        public override void Update(GameTime gameTime)
        {
            if (showHUD)
            {
                elapsedTime += gameTime.ElapsedGameTime;
                updateCounter++;

                if (elapsedTime > TimeSpan.FromSeconds(1))
                {
                    elapsedTime -= TimeSpan.FromSeconds(1);
                    frameRate = frameCounter;
                    updateRate = updateCounter;
                    frameCounter = 0;
                    updateCounter = 0;

                    fps = string.Format("fps: {0}", frameRate);
                    ups = string.Format("ups: {0}", updateRate);
                }

                drewModels = shapeController.getDrewModels;
                notDrewModels = shapeController.getNotDrewModels;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            if (showHUD)
            {
                frameCounter++;
                
                spriteBatch.DrawString(spriteFont, fps, new Vector2(5, 5), Color.White);
                spriteBatch.DrawString(spriteFont, ups, new Vector2(5, 15), Color.Red);
                spriteBatch.DrawString(spriteFont, drewModels, new Vector2(5, 30), Color.White);
                spriteBatch.DrawString(spriteFont, notDrewModels, new Vector2(5, 40), Color.Red);
                spriteBatch.DrawString(spriteFont, "Mouse.X: " + mousePositionX, new Vector2(5, 55), Color.LightGreen);
                spriteBatch.DrawString(spriteFont, "Mouse.Y: " + mousePositionY, new Vector2(5, 65), Color.LightGreen);
                
            }
            spriteBatch.DrawString(spriteFont, "Core Update Time: " + CoreUpdate, new Vector2(5, 75), Color.Coral);
            spriteBatch.DrawString(spriteFont, "Core Draw Time: " + CoreDraw, new Vector2(5, 85), Color.Coral);
            spriteBatch.DrawString(spriteFont, "InputController Update Time: " + InputControllerUpdate, new Vector2(5, 95), Color.Coral);
            spriteBatch.End();
        }

        #endregion

    }
}
